package com.jianbo.util;

import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

import org.noear.snack.ONode;
import org.noear.snack.core.Feature;
import org.noear.snack.core.Options;

import com.jianbo.proxy.util.ByteUtil;
import com.jianbo.proxy.util.Debug;
import com.jianbo.proxy.util.FileUtil;
import com.jianbo.proxy.util.JsonUtil;
import com.jianbo.proxy.util.ResourceUtil;
import com.jianbo.proxy.util.HttpUtil.HttpResult;

public class AutoCode {
    
    public static void main(String[] args) {
        URL url = ResourceUtil.getResource("contentType.json");
        Debug.info(url.getFile());
        Debug.info(url.getFile().substring(url.getFile().lastIndexOf(".")));
    }

    public static void saveContentType(HttpResult httpResult, String filePath) {
        Map<String, String> header = httpResult.getResponseHeader();
        String contentType = header.get("Content-Type");
        String fileType = FileUtil.fileType(filePath);
        String json = ResourceUtil.readString("contentType.json");
        LinkedHashMap<String, Object> map = JsonUtil.parseSortMap(json);
        map.put(fileType, contentType);
        json = ONode.stringify(map, Options.of(Feature.PrettyFormat));
        URL url = ResourceUtil.getResource("contentType.json");
        String jsonFile = url.getFile().replace("target/classes/", "src/main/resources/");
        FileUtil.saveFile(jsonFile, ByteUtil.bytes(json));
        Debug.info(fileType + "->" + contentType);
    }
}
